<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style>
        .img-area{
                width: 150px;
                height: 150px;
                margin: 10px;
                position: relative;
                display: none;
            }
            .img-area i{
                position: absolute;
                right: 5px;
                top:2px;
                color: white;
                z-index: 1;
                cursor: pointer;
            }
            .img-area img{
               width: 100%;
               height: 100%;
               border-radius: 5px;
               display: block;
               border: 1px solid;
               object-fit: contain;
               box-sizing: border-box;
    
            }
            .img-area::after{
                content: "";
                position:absolute;
                height: 20px;
                background-color:rgba(0, 0, 0, 0.3);
                top: 0;
                right: 0;
                width: 100%;
                border-radius:5px 5px 0 0;
            }
            
        
        
        </style>
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>
            <a href="./cinemaSelect.html" class="layui-btn layui-btn-sm" id="back">返回</a>
            添加院线
        </legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="cinemaForm" onsubmit="return false">

        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">院线名称</label>
                <div class="layui-input-block">
                    <input type="tel" name="name" placeholder="请输入院线名称" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">院线地址</label>
                <div class="layui-input-block">
                    <input type="text" name="addr" placeholder="请输入院线地址" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">图片</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="btnUpload">
                        <i class="layui-icon">&#xe67c;</i>图片上传
                    </button>
                </div>
            </div>
            <div class="layui-block">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <div class="img-area">
                        <img src="" alt="">
                        <i class="layui-icon layui-icon-close"></i>
                    </div>
                </div>
            </div>
            <div class="layui-block">
                <div class="layui-input-block">
                    <input type="text" name="image" placeholder="请选择上传图片" lay-verify="required" autocomplete="off"
                        class="imageTxt layui-input" style="display:none">
                </div>
            </div>
        </div>

        </div>


        <div class="layui-form-item">
            <div class="layui-block">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-block">
                    <input type="text" name="tel" placeholder="请输入院线联系电话" lay-verify="required|phone" autocomplete="off"
                        class="layui-input">
                </div>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="cinema-add">添加院线</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="reset">重置</button>
            </div>
        </div>


    </form>
    <script src="../../js/lodash.min.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>

    <script>



        layui.use("upload", function () {
            let upload = layui.upload;
            let uploadInst = upload.render({
                elem: '#btnUpload',
                url: "/api/upload/",
                field: "imgfile",
                drag: true,
                done: function (res) {
                    $(".img-area img").prop("src", res.data[0]);
                    $(".imageTxt").val(res.data[0]);
                    $(".img-area").show();

                },
                error: function () {
                    console.log("文件错误");
                }
            })
        });

        $(".img-area i").click(function () {
            $(".img-area img").prop("src", "");
            $(".imageTxt").val("");
            $(".img-area").hide();
        })

        layui.use(['form'], async function () {
            const form = layui.form;

            //监听提交
            form.on('submit(cinema-add)', async function (data) {
                console.log(data.field);
                await $.post("/api/Cinema", data.field);
                layer.msg('添加院线成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    location.href = "./select.html";
                });
            });


            form.render("select");

            $("#back").onclick = function () {
                location.href = "./select.html";
            }
        });
    </script>

</body>

</html>